package com.huangjue.backend.mapper;

import com.huangjue.backend.entity.AiProgrammingConversation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

@Mapper
public interface AiProgrammingConversationMapper {
    int insertConversation(AiProgrammingConversation conversation);
    int updateConversation(AiProgrammingConversation conversation);
    int deleteConversation(@Param("conversationId") Integer conversationId);
    AiProgrammingConversation selectByConversationId(@Param("conversationId") Integer conversationId);
    List<AiProgrammingConversation> selectByUserAndHomework(
            @Param("userId") String userId,
            @Param("homeworkId") String homeworkId);
    List<AiProgrammingConversation> selectByRoundRange(
            @Param("userId") String userId,
            @Param("homeworkId") String homeworkId,
            @Param("startRound") Integer startRound,
            @Param("endRound") Integer endRound);

    // 获取用户最近对话记录
    List<AiProgrammingConversation> selectLatestConversations(
            @Param("userId") String userId,
            @Param("homeworkId") String homeworkId,
            @Param("limit") int limit  // 新增参数
    );

    // 查询指定用户和作业的最大轮次号
    @Select("SELECT MAX(round_number) FROM ai_programming_conversations " +
            "WHERE user_id = #{userId} AND homework_id = #{homeworkId}")
    Integer selectMaxRoundNumber(@Param("userId") String userId,
                                        @Param("homeworkId") String homeworkId);

    List<AiProgrammingConversation> selectByFileLinkAndDate(
            @Param("filePrefix") String filePrefix,
            @Param("startDate") Date startDate,
            @Param("endDate") Date endDate
    );
}